package com.yidu.BusinessData.controller;

import com.yidu.BusinessData.entity.AssetValuationData;
import com.yidu.BusinessData.entity.assetValuation;
import com.yidu.BusinessData.mapper.assetValuationMapper;
import com.yidu.BusinessData.service.assetValuationService;
import com.yidu.BusinessData.util.IdUtil;
import com.yidu.BusinessManagement.entity.DealData;
import com.yidu.InventoryManagement.entity.SecurityAccrualStock;
import com.yidu.InventoryManagement.service.cashAccrualStockService;
import com.yidu.InventoryManagement.service.securityAccrualStockService;
import com.yidu.TAManagement.entity.Ta;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author z
 * @version 1.0
 * @date 2022/12/12
 */
@RestController
public class assetValuationController {
    //调用资产估值的service类
    @Resource
    private assetValuationService assetValuationService;
    //调用资产估值Dao类
    @Resource
    assetValuationMapper assetValuationMapper;
    //调用证券应收应付库存service类
    @Resource
    securityAccrualStockService securityAccrualStockService;
    //调用现金应收应付库存service类
    cashAccrualStockService cashAccrualStockService;


    /**
     * 资产估值查询方法
     *@param assetDate 业务日期
     *  @param   assetValuationId 资产估值编号
     * @return
     */
    @RequestMapping("selectAssetValuationData")
    public Map selectAssetValuationData(String assetDate, String assetValuationId) {
        return assetValuationService.selectAssetValuation(assetDate, assetValuationId);
    }

    /**
     * 资产估值操作类
     *
     * @param assetDate 业务日期
     * @param arrJson   json数据
     * @return
     */
    @RequestMapping("startValuation")
    //业务日期 json数据
    public int startValuation(String assetDate, String arrJson) {
        System.out.println("进来了");
        System.out.println(arrJson + " " + assetDate);
        int i = 0;
       //list集合
        List<AssetValuationData> assetValuationDataList = IdUtil.jsonToArrayList(arrJson, AssetValuationData.class);
        for (AssetValuationData assetValuationData : assetValuationDataList) {
            if (assetValuationData.getSecurityType() == 1) {
                System.out.println("证券估值增值开始估值");
                //基金代码  业务日期
                //连接那几张表
                List<assetValuation> select = assetValuationService.select(assetDate);

                int s = securityAccrualStockService.selectSecurityAccrualStockId();
                System.out.println(s+"]]]]]]]]");
                String id1=null;
                if(s==0){
                     id1="SA2022120511";

                }else{
                    String s1 = securityAccrualStockService.getmaxsecurityAccrualStockId().substring(2,12);
                    System.out.println(s1+"SSSSSS");
                    com.yidu.InventoryManagement.Util.IdUtil idUtil=new com.yidu.InventoryManagement.Util.IdUtil();
                     id1 = "SA"+idUtil.createID(s1);

                }
                System.out.println("assetValuationMap=" + select);
                for (assetValuation assetValuations : select) {
                    double v = assetValuationService.selectMoneyByDate(assetDate);
                    //将估值的数据添加到证券应收应付库存
                    SecurityAccrualStock stock = new SecurityAccrualStock();

                    //开始执行增加
                    //得到证券应收应付id
                    stock.setSecurityAccrualStockId(id1);
                    //得到基金id
                    stock.setFundId(assetValuations.getFundId());
                    //得到securityId
                    stock.setSecurityId(assetValuations.getSecurityId());
                    //得到日期
                    stock.setDate(assetDate);
                    //类型
                    stock.setSecurityType(1);
                    //初期状态
                    stock.setInitialState(1);
                    //业务状态
                    stock.setBusinessState(1);
                    //总金额
                    stock.setMoney(v);
                    //备注
                    stock.setRemark("投资有风险");
                    System.out.println("增加的实体类=" + stock);
                    //执行删除
                    i = assetValuationService.deleteSecuritiesInventoryInfos(stock);
                    //调用增加方法
                    securityAccrualStockService.addSecuritiesInventoryInfo(stock);
                }
            } else {
                int s = securityAccrualStockService.selectSecurityAccrualStockId();
                System.out.println(s+"]]]]]]]]");
                String id1=null;
                if(s==0){
                    id1="SA2022120511";

                }else{
                    String s1 = securityAccrualStockService.getmaxsecurityAccrualStockId().substring(2,12);
                    System.out.println(s1+"SSSSSS");
                    com.yidu.InventoryManagement.Util.IdUtil idUtil=new com.yidu.InventoryManagement.Util.IdUtil();
                    id1 = "SA"+idUtil.createID(s1);

                }
                System.out.println("清算款清算中");
                //查交易数据 按证券代码分组 插入证券应收应付库存
                List<DealData> assetValuations = assetValuationService.selectAssetValuationss(assetDate);
                 //交易数据 list集合
                List<DealData> assetValuationDataLists = IdUtil.jsonToArrayList(arrJson, DealData.class);
                for (DealData assetValuationDatas : assetValuations) {
                    System.out.println("DealData=================" + assetValuationDatas);
                            SecurityAccrualStock stocks = new SecurityAccrualStock();
                           //得到证券应收应付id
                            stocks.setSecurityAccrualStockId(id1);
                            //日期
                            stocks.setDate(assetDate);
                            //基金id
                            stocks.setFundId(assetValuationDatas.getFundId());
                            //证券类型
                            stocks.setSecurityType(2);
                            //证券编号
                            stocks.setSecurityId(assetValuationDatas.getSecurityId());
                            //交易总金额
                            Double endMoney = 0.0;
                            System.out.println("插入=" + assetValuationDatas);
                          //  assetValuationMapper.selectDealData(null);
                            if (assetValuationDatas.getEndMoney()<0) {
                                //资金流向为-1时  总金额为负 拿金额乘以-1得到正确的金额
                                endMoney = assetValuationDatas.getEndMoney()*-1;
                                stocks.setBusinessState(-1);
                            } else {

                                endMoney = assetValuationDatas.getEndMoney();
                                stocks.setBusinessState(1);
                            }
                            //        System.out.println("类型="+transactionData.getFlag());
                            stocks.setMoney(endMoney);
                            stocks.setInitialState(2);
                            stocks.setRemark("投资有风险");
                            System.out.println("查清算款增加的实体类=" + stocks);
                            assetValuationService.deleteSecuritiesInventoryInfoTwo(stocks);
                            //调用证券应收应付库存增加方法，将清算的数据插入到证券应收应付库存
                            securityAccrualStockService.addSecuritiesInventoryInfo(stocks);
                            System.out.println("查ta交易数据================================");

                        }
                        }
                     }
                return i;
            }

}
